
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>

#define SERVER_PORT 9898
#define MAX_RECVBUF 5

#define MAX_EVENTS 100

int main(void)
{

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd)
    {
        perror("socket create: ");
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in serverAddr;
    socklen_t sAddrLen = sizeof(serverAddr);
    memset(&serverAddr, 0, sAddrLen);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    // 设置端口复用
    int opt = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
    {
        perror("setsockopt error: ");
        exit(EXIT_FAILURE);
    }

    if (bind(sockfd, (struct sockaddr *)&serverAddr, sAddrLen) == -1)
    {
        perror("bind error: ");
        exit(EXIT_FAILURE);
    }

    if (listen(sockfd, 10) < 0)
    {
        perror("listen error: ");
        exit(EXIT_FAILURE);
    }

    // 创建 epoll 句柄
    int epoll_fd = epoll_create(10);
    if (0 > epoll_fd)
    {
        perror("epoll_create error: ");
        exit(EXIT_FAILURE);
    }

    // 添加监听事件
    struct epoll_event ev;
    ev.data.fd = sockfd;
    ev.events = EPOLLIN | EPOLLET; // 设置边沿触发
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &ev) != 0)
    {
        perror("epoll_ctl add error: ");
        exit(EXIT_FAILURE);
    }

    struct epoll_event evs[MAX_EVENTS];
    while (1)
    {
        int ret = epoll_wait(epoll_fd, evs, MAX_EVENTS, -1); // 永久阻塞等待事件触发
        if (0 < ret)
        {
            for (int i = 0; i < ret; ++i)
            {
                if (!(evs[i].events & EPOLLIN))
                {
                    continue;
                }

                int fd = evs[i].data.fd;
                if (fd == sockfd) // 新客户端接入
                {
                    int cfd = accept(sockfd, NULL, NULL);
                    if (0 > cfd)
                    {
                        perror("accept error: ");
                        exit(EXIT_FAILURE);
                    }

                    // 设置文件cfd为非阻塞模式
                    int flag = fcntl(cfd, F_GETFL);
                    flag |= O_NONBLOCK;
                    fcntl(cfd, F_SETFL, flag);

                    ev.data.fd = cfd;
                    ev.events = EPOLLIN | EPOLLET;
                    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, cfd, &ev) != 0)
                    {
                        perror("epoll_ctl add error: ");
                        exit(EXIT_FAILURE);
                    }
                }
                else
                {
                    char buff[MAX_RECVBUF];
                    int len;
                    while ((len = recv(fd, buff, MAX_RECVBUF, 0)) > 0)
                    {
                        write(STDOUT_FILENO, buff, len);
                        send(fd, buff, len, 0);
                    }

                    if (len == 0)
                    {
                        printf("客户端离开\n");
                        ev.data.fd = fd;
                        ev.events = EPOLLIN;
                        if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) != 0)
                        {
                            perror("epoll_ctl add error: ");
                            exit(EXIT_FAILURE);
                        }
                    }
                    else if(len == -1)
                    {
                        if(errno == EAGAIN)
                        {
                            printf("缓冲区数据读完");
                        }
                        else{
                            perror("recv error: ");
                            exit(EXIT_FAILURE);
                        }
                    }
                }
            }
        }
    }

    return 0;
}